package com.study.lihan.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author: lihan
 * @Date : 2022/1/10- 01
 * @DESCRIPTION: com.study.lihan.io
 * @version: 1.0
 */
public class BIO {
    public static void main (String[] args) throws  Exception{
        ServerSocket server = new ServerSocket(9090);
        System.out.println("step1： new");
        while (true){
//            System.in.read();
           final Socket client = server.accept(); //阻塞1
            System.out.println("step2: "+ client.getPort() );
            new Thread(()->{
                InputStream in = null;
                try {
                    in = client.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    while (true){
  //                      System.in.read();
                        String dataline = reader.readLine();//阻塞2
                        if (dataline != null){
                            System.out.println(dataline);
                        }else{
                            client.close();
                            break;
                        }

                    }
                    System.out.println("客户端断开连接");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();

        }
    }
}
